﻿<%@ WebHandler Language="C#" Class="ZSXinJiStepPlanDashBoard" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using ReportBusiness;
using System.Net;

/*
 * Description: 装配厂--改复装机计划进度看板(新机看板)
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2023.7.25
 */

public class ZSXinJiStepPlanDashBoard : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "getDetail":
                        r = getDetail(jObject);
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    //柱状图
    Result getData(JObject jObject)
    {
        string sql = @"
SELECT t.engineno
FROM 
(
    SELECT zp.engineno,SUM(DECODE(zp.factfinisheddate,NULL,0,1)) finished,COUNT(1) qty
    FROM zsstepdispatchplan zp
    LEFT JOIN workflowstep ws ON ws.workflowstepid = zp.workflowstepid
    WHERE ws.workflowstepname IS NOT NULL
    GROUP BY zp.engineno
) t 
WHERE t.finished <> t.qty";
        if (jObject["engineno"] != null && jObject["engineno"].ToString() != "")
        {
            sql += string.Format(" AND t.engineno = '{0}'", jObject["engineno"].ToString());
        }
        Result r = new Result();
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    Result getDetail(JObject jObject)
    {
        string sql = @"
SELECT zs.engineno,ws.sequence,ws.workflowstepname,st.stepno,nvl(st.stepno,1) I,zs.stepinfodesc,
       zs.plannedstartdate,zs.plannedfinishdate,
       TRUNC(zs.plannedfinishdate - zs.plannedstartdate) + 1 || '天' cycle,
       CASE WHEN zs.factfinisheddate IS NOT NULL THEN to_char(zs.factfinisheddate,'yyyy-mm-dd')
            WHEN zs.factfinisheddate IS NULL AND TRUNC(SYSDATE) < TRUNC(zs.plannedstartdate) 
              THEN '未完成' || '(' || (TRUNC(zs.plannedfinishdate - zs.plannedstartdate) + 1 || '天') || ')'
            WHEN zs.factfinisheddate IS NULL AND TRUNC(SYSDATE) >= TRUNC(zs.plannedstartdate) AND TRUNC(SYSDATE) <= TRUNC(zs.plannedfinishdate)
              THEN '未完成' || '(' || (zs.plannedfinishdate - TRUNC(SYSDATE) + 1 || '天') || ')' 
            WHEN zs.factfinisheddate IS NULL AND TRUNC(SYSDATE) > TRUNC(zs.plannedfinishdate) THEN '未完成(已超期)'
       END finishdate,
       zs.exceednotes,t.stepCount,
       CASE WHEN zs.factfinisheddate IS NOT NULL THEN 'green'
            WHEN zs.factfinisheddate IS NULL AND TRUNC(SYSDATE) > TRUNC(zs.plannedfinishdate) THEN 'red'
       ELSE '' END color
FROM zsstepdispatchplan zs
LEFT JOIN Workflowstep ws ON ws.workflowstepid = zs.workflowstepid
LEFT JOIN stepinfo st ON st.stepinfoid = zs.stepinfoid
LEFT JOIN 
(
     SELECT zs.engineno,zs.workflowstepid,COUNT(1) stepCount
     FROM zsstepdispatchplan zs
     GROUP BY zs.engineno,zs.workflowstepid
) t ON t.engineno = zs.engineno AND t.workflowstepid = zs.workflowstepid
WHERE ws.workflowstepname IS NOT NULL AND zs.engineno = '{0}'
ORDER BY zs.engineno,ws.sequence,st.stepno";
        sql = string.Format(sql, jObject["engineno"].ToString());
        Result r = new Result();
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        r.data = dt;
        r.total = Convert.ToInt32(dt.Rows.Count);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }
}